﻿CREATE PROCEDURE [dbo].[yaf_watchtopic_list](
                @UserID INT)
AS
    BEGIN
        SELECT a.*,
               TopicName = b.Topic,
               Replies = (SELECT COUNT(1)
                          FROM   yaf_Message x
                          WHERE  x.TopicID = b.TopicID),
               b.Views,
               b.LastPosted,
               b.LastMessageID,
               b.LastUserID,
               LastUserName = Isnull(b.LastUserName,(SELECT Name
                                                     FROM   yaf_User x
                                                     WHERE  x.UserID = b.LastUserID))
        FROM   yaf_WatchTopic a,
               yaf_Topic b
        WHERE  a.UserID = @UserID
        AND b.TopicID = a.TopicID
    END


